﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BusinessLayer;
using EntityModel;
using System.IO;

namespace PL___Windows_Inteface
{
    public partial class ManageTrucks : Form
    {
        private Truck truck;
        private Bitmap img;
        public ManageTrucks(string operation, string regnumber)
        {
            InitializeComponent();
            BL controller = new BL();
            foreach (Fuel f in controller.getFuelTypes())
            {
                fuel_cb.Items.Add(f.fuel_name);
            }
            foreach (States s in controller.getVehicleStates())
            {
                states_cb.Items.Add(s.state_name);
            }  
            
            if (operation == "ADD")
            {  

                viewimage_bt.Visible = false;
                delete_bt.Visible = false;
                save_changes_bt.Visible = false;
              
               

            }
            else if (operation == "VIEW")
            {
                
                add_image_bt.Visible = false;
                submit_bt.Visible = false;
                
                 truck = controller.getTruck(regnumber);              
                 fuel_cb.Text = controller.getFuelName(Convert.ToInt32(truck.Vehicle.fuel));
                 states_cb.Text = controller.getStateName(Convert.ToInt32(truck.Vehicle.state));              
                 color_tb.Text = truck.Vehicle.colour;
                 price_tb.Text= truck.Vehicle.price.ToString();
                 reg_number_tb.Text = truck.Vehicle.reg_number;
                 num_wheels_tb.Text = truck.num_wheels.ToString();
                 tonnage_tb.Text=truck.tonnage.ToString();
                 high_tb.Text=truck.high.ToString();
                 width_tb.Text=truck.width.ToString();
                 large_tb.Text=truck.large.ToString();
                 if (truck.Vehicle.v_image != null)
                 {
                     Byte[] a = truck.Vehicle.v_image.ToArray();
                     img = new Bitmap(Bytes2Image(a));
                 }
               
            }
        }
        private void submit_bt_Click(object sender, EventArgs e)
        {
            Button bt = (Button)sender;
            if (bt.Text == "Submit")
            {
                truck = new Truck();
                truck.Vehicle = new Vehicle();
                truck.Vehicle.type_vehicle = 2;
            }
            truck.Vehicle.reg_number = reg_number_tb.Text.ToString();


            BL controller = new BL();
            truck.Vehicle.fuel = controller.getFuelId(fuel_cb.SelectedItem.ToString());
            try { truck.Vehicle.price = Convert.ToInt32(price_tb.Text.ToString()); }
            catch { error_lb.Text = "The price must be number"; }
            truck.Vehicle.colour = color_tb.Text.ToString();
            truck.Vehicle.state = controller.getStateId(states_cb.SelectedItem.ToString());

            try { truck.num_wheels = Convert.ToInt32(num_wheels_tb.Text.ToString()); }
            catch { error_lb.Text = "The wheels number must be number"; }

            try { truck.tonnage = Convert.ToInt32(tonnage_tb.Text.ToString()); }
            catch { error_lb.Text = "The tonnage must be number"; }

            try { truck.high = Convert.ToInt32(high_tb.Text.ToString()); }
            catch { error_lb.Text = "Hight value must be number"; }
            try { truck.width = Convert.ToInt32(width_tb.Text.ToString()); }
            catch { error_lb.Text = "Width value must be number"; }
            try { truck.large = Convert.ToInt32(large_tb.Text.ToString()); }
            catch { error_lb.Text = "Large value must be number"; }
            Byte[] result = (Byte[])new ImageConverter().ConvertTo(img, typeof(Byte[]));
            truck.Vehicle.v_image = new System.Data.Linq.Binary(result);



            if (bt.Text == "Submit")
            {
                controller.saveTruck(truck);
                error_lb.ForeColor = Color.Blue;
                error_lb.Text = "Truck added correctly";
            }
            else
            {
                controller.updateTruck(truck);
                error_lb.ForeColor = Color.Blue;
                error_lb.Text = "Truck updated correctly";

            }
        }


        private void delete_bt_Click(object sender, EventArgs e)
        {
            BL controller = new BL();
            try
            {
                controller.deleteTruck(truck);
                MessageBox.Show("Truck Deleted Correctly", "Deleted", MessageBoxButtons.OK);
                this.Close();
            }
            catch (Exception ex)
            {
                error_lb.Text = ex.Message;

            }
        }

        private void viewimage_bt_Click(object sender, EventArgs e)
        {
            if (img != null)
            {
                (new ImageForm(img)).ShowDialog();
            }
            else
            {
                MessageBox.Show("No picture avaliable");
            }

        }
        public static Image Bytes2Image(byte[] bytes)
        {
            if (bytes == null) return null;
            //
            MemoryStream ms = new MemoryStream(bytes);
            Bitmap bm = null;
            try
            {
                bm = new Bitmap(ms);
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine(ex.Message);
            }
            return bm;
        }

        private void add_image_bt_Click(object sender, EventArgs e)
        {

            OpenFileDialog dialog = new OpenFileDialog();
            dialog.Title = "Select an Image to add in the Truck register";
            if (dialog.ShowDialog() == DialogResult.OK)
            {

                img = new Bitmap(dialog.FileName);

            }
            viewimage_bt.Visible = true;
            add_image_bt.Visible = false;
        }
    }
}
